package cn.ad.controller;

import cn.ad.common.Constants;
import cn.ad.common.DataUtils;
import cn.ad.model.AdSpaceModel;
import cn.ad.model.DropDownModel;
import cn.ad.model.ResultModel;
import cn.ad.param.AdSpaceQuery;
import cn.ad.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 广告位API
 */
@Controller
@RequestMapping("adSpace")
public class AdSpaceController {
	
	@Autowired
	private AdSpaceService adSpaceService;

	@Autowired
	private CreativeService creativeService;

	@Autowired
	private AdSpaceReportService adSpaceReportService;

	@Autowired
	private MediaService mediaService;

	@Autowired
	private AdvertiserService advertiserService;
	
	/**
	  * 根据查询条件查询广告位列表
	  */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public ResultModel findAdvertisers(AdSpaceQuery query, @RequestHeader(value = "Referer", required = false) String header) {
		int searchStatus = 0;
		if (header.contains(Constants.URI_VIEWS_STRATEGY_EDIT)) {
			searchStatus = 1;
		}
		Map<String, Object> params = new HashMap<>();
		params.put("strategyGroupId", query.getStrategiesGroupId());
		params.put("resType", query.getResourceType());
		List<DropDownModel> dropDownModels = mediaService.selectPubSiteList(params);
		List<Integer> pubsitIds = new ArrayList<>();
		for (DropDownModel dropDownModel : dropDownModels) {
			pubsitIds.add(dropDownModel.getId());
		}
		query.setPubSiteIds(pubsitIds);
		return new ResultModel(adSpaceService.findAdSpaces(query.getSearchMap(), query.getPageInfo(), searchStatus));
	}

//	/**
//	 * 保存广告位
//	 *
//	 * @param params
//	 * @return
//	 */
//	@RequestMapping(method = { RequestMethod.POST, RequestMethod.PUT })
//	public ModelAndView saveSpace(@RequestBody AdSpaceParams params) {
//		if(params.getId()!=0){
//			if(booleanStatusChange(params.getId(),params.getStatus())){
//				adSpaceService.saveSapce(ConvertObject.ConvertToBean(params, AdSpace.class),false);
//			}
//		}else{
//			adSpaceService.saveSapce(ConvertObject.ConvertToBean(params, AdSpace.class),true);
//		}
//
//		return RestfulJsonModelAndView.buildJsonModelAndView();
//	}
//
//	/**
//	 * 广告位编辑获取单个广告位信息接口
//	 * @param id 广告位ID
//	 * @return
//	 */
//	@RequestMapping(value = "{id}",method = RequestMethod.GET)
//	public ModelAndView findAdSpaceInfo(@PathVariable("id") int id){
//		AdSpaceInfoModel adSpace=adSpaceService.findAdSpaceInfoById(id);
//		//Integer templateId=adSpaceService.findAdSpaceTemplateId(adSpace.getDefaultCreativeId());
//		//adSpace.setTemplateId(templateId);
//		return RestfulJsonModelAndView.buildJsonModelAndView(adSpace);
//	}
//
//	/**
//	 * 广告位状态快捷修改接口
//	 * @param id
//	 * @param status
//	 * @return
//	 */
//	@RequestMapping(value = "{id}/status",method = RequestMethod.PUT)
//	public ModelAndView updateStatus(@PathVariable("id") int id, @RequestBody Map<String,Integer> status){
//		if(booleanStatusChange(id, status.get("status"))){
//			adSpaceService.updateAdSpaceStatus(id,status.get("status"));
//		}
//		return RestfulJsonModelAndView.buildJsonModelAndView();
//	}
//
//	/**
//	 * 验证状态是否可以更改
//	 * @param id
//	 * @param status
//	 * @return
//	 */
//	private boolean booleanStatusChange(int id, int status) {
//		//根据id获取广告位信息
//		AdSpaceInfoModel adSpace = adSpaceService.findAdSpaceInfoById(id);
//		if(adSpace.getStatus()==status){
//			return true;
//		}
//		int count=0;
//		if (status==0){
//			count=adSpaceRptSerivce.getAdSpaceRptDataCount(id);
//		}
//		if(count==0){
//			return true;
//		}else{
//			throw new AdSpaceValidateException(AdSpaceValidateException.ErrorCode.ADSPACE_CHANGE_FAIL);
//		}
//	}
//
//
//
//	/**
//	 * 删除广告位接口
//	 * @param id
//	 * @return
//     */
//	@RequestMapping(value = "{id}",method = RequestMethod.DELETE)
//	public ModelAndView deleteAdSpace(@PathVariable("id") int id){
//		int count=adSpaceRptSerivce.getAdSpaceRptDataCount(id);
//		if (count>0){
//			throw new AdSpaceValidateException(AdSpaceValidateException.ErrorCode.ADSPACE_DELETE_FAIL);
//		}
//		adSpaceService.deleteSapce(id);
//		return RestfulJsonModelAndView.buildJsonModelAndView();
//	}
//
//	/**
//	 * 根据广告形式和模板id查询符合条件的创意
//	 * @param query
//	 * @return
//	 */
//	@RequestMapping(value = "creatives",method = RequestMethod.GET)
//	public ModelAndView getCreativeListByFormatId(CreativeQueryParam query){
//		List<CreativeDefaultModel> list = creativeService.getCreativeListByFomartId(query.getFormatId(),query.getTemplateId());
//		ResultList<CreativeDefaultModel> model =  new ResultList<>();
//		model.setList(list);
//		model.setTotal_number(list.size());
//		return  RestfulJsonModelAndView.buildJsonModelAndView(model);
//	}
}
